package activity.lsen.smartwsn.com.bluetoothdemo.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import activity.lsen.smartwsn.com.bluetoothdemo.R;

/**
 * Created by thinkpad on 2017/3/11.
 */

public class ListViewAdapter  extends BaseAdapter{

    private List<BluetoothDevice> deviceList;
    private Context context;

    public ListViewAdapter(Context context,List<BluetoothDevice> list) {
        this.context = context;
        this.deviceList=list;
    }

    public void notifyData(List<BluetoothDevice> list){
        this.deviceList=list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        if(deviceList.size()>0) {
            BluetoothDevice device=deviceList.get(position);
            viewHolder.deviceTv.setText(device.getName().toString());
            viewHolder.macTv.setText(device.getAddress().toString());
        }
        return convertView;
    }

     class ViewHolder {
           TextView deviceTv;
           TextView macTv;

           ViewHolder(View view) {
               deviceTv=(TextView)view.findViewById(R.id.device_name);
               macTv=(TextView)view.findViewById(R.id.device_mac);
           }
       }

}
